package alf.mapping.view.activity;

import android.widget.*;

import alf.mapping.dto.maps.Item;
import alf.mapping.dto.maps.Link;
import alf.mapping.view.Env;
import alf.mapping.view.activity.R;
import alf.mapping.view.visuals.EditorEngine;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class LinkSelectionActivity extends SimpleAbstractActivity {

	EditorEngine editor;
	private ListView lista;
	Link selected;
	int pos= -1;
	Menu menu;
	private ListAdapter adapter;

	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.link_selection);
		editor = EditorEngine.getInstance();
		lista = (ListView) findViewById(R.id.link_list);
		lista.setChoiceMode(lista.CHOICE_MODE_SINGLE);
		updateListData();

		lista.setOnItemClickListener(new ListView.OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					//	editLink(position);
					//evento("itemclick:", position);
					checkedChanged();
				}
			});
	}

	private void evento(String evento, int position) {
		Env.toast(this, evento);
	}

	private void updateListData() {
		String[] names;
		Item item = editor.getSelected().get(0).getItem();
		if (item.getLinks() == null) {
			names = new String[0];
		} else {
			names = new String[item.getLinks().size()];

			for (int i=0;i < names.length;i++) {
				names[i] = item.getLinks().get(i).toString();
			}
		}
		adapter =  new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice, names);
		lista.setAdapter(adapter);
	}
	@Override
	public void onWindowFocusChanged(boolean focus) {
		super.onWindowFocusChanged(focus);
		if (focus && adapter != null) {
			updateListData();
		}
	}

	void editLink(int position) {
		Env.toast(this, "pos " + position);
		Intent i = new Intent(this, LinkEditionActivity.class);
		Uri.Builder b = new Uri.Builder().appendPath("link");

		b.appendQueryParameter("index", String.valueOf(position));
		i.setData(b.build());
		startActivity(i);
	}


	void deleteLink(int position) {
		Item item = editor.getSelected().get(0).getItem();
		item.getLinks().remove(position);
		if (item.getLinks().size() == 0) {
			item.setLinks(null);
		}
		updateListData();
	}

	public void onOk(View v) {
		finish();
	}	

	@Override 
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent i;
		int pos = lista.getCheckedItemPosition();
		switch (item.getItemId()) {
			case R.id.m_new:
				newLink();
				break;
			case R.id.m_edit:
				if (pos != ListView.INVALID_POSITION) {
					editLink(pos);
				}
				break;
			case R.id.m_delete:
				if (pos != ListView.INVALID_POSITION) {
					deleteLink(pos);
				}
				//	Env.toast(this, "delete not implemented yet");
				break;
			default:
				super.onOptionsItemSelected(item);
		}
		return true;
	}

	private void newLink() {
		Link link = new Link();
		Item item = editor.getSelected().get(0).getItem();
		item.add(link);
		editLink(item.getLinks().size() - 1);
	}

	@Override 
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		getMenuInflater().inflate(R.menu.edit_menu, menu);
		this.menu = menu;
		return true;
	}

	void checkedChanged() {
		// activates edit menu if item is checked
		pos = lista.getCheckedItemPosition();
		menu.setGroupVisible(R.id.m_actions, pos != ListView.INVALID_POSITION);
	}

}
